<?php

namespace app\admin\controller;
use app\admin\model\FastCar as FastCarModel;
use my\Tools;
use think\Request;
use think\Db;
use app\admin\model\RegionModel;
use think\Cache;
use think\mongo\Connection;
use my\Redis;
use my\Security;

class DriverLator extends Base {

    protected $fast_car_model;
    protected $lat_model;
    protected $lng_model;
    protected $distance_model;

    public function _initialize() {
        parent::_initialize();
        $this->fast_car_model = new FastCarModel();
        $this->region_model         = new RegionModel();
        $provinceList = $this->region_model->getRegion(['parent_id'=>1,'region_level'=>1],'region_id,region_name');
        $this->assign('provinceList',$provinceList);
        $this->lat_model =31.859653;
        $this->lng_model =117.281553;
        $this->distance_model =20000;
    }
    /*
     * 司机时时位置数据
     * city_id 城市ID
    */
    public function translator(){
        $drivetType = input('post.drivetType');
        $highLight  = input('id');
        if(!empty($drivetType)){

            $data['driver_type']=$drivetType;
            $request                = Request::instance();
            $data['province_id']    = input('post.province_id')?input('post.province_id'):13;
            $data['city_id']        = input('post.city_id')?input('post.city_id'):133;
            $cityLocationKey        = 'cityLocationKey:'.$data['city_id'];

            $data=$this->work_city($data['city_id'],$drivetType,null,$_POST['stime']);
            echo json_encode($data);die;
            $cityLocalInfo          = Cache::get($cityLocationKey);
            if(!empty($cityLocalInfo)){
                $location           = $cityLocalInfo;
            }else{
                $cityName           = Db::name('region')->where(['region_id'=>$data['city_id']])->value('region_name');
                $url                = "http://api.map.baidu.com/geocoder?address=".$cityName."&output=json&key=XgrsTKYvYuiq9wa2BUrfNXOW37E1DSdy&city=".$cityName;
                $result             = file_get_contents($url);
                $json               = json_decode($result,true);
                $location           = $json['result']['location'];
                Cache::set($cityLocationKey,$location);
            }
            $driverList             = $this->fast_car_model->driverListMongo($data);

            //开始处理司机 - 数据
            $driverArr = $driverLocationArr = [];
            foreach ($driverList as $key=>$value){
                $driverArr[]        = $value['driverId'];
                $driverLocationArr[$value['driverId']]['lng'] = $value['lng'];
                $driverLocationArr[$value['driverId']]['lat'] = $value['lat'];
            }
            //开始缓存司机信息
            $driverInfoKey          = 'driverlocation:info';
            $driverInfo             = Cache::get($driverInfoKey);//获取缓存中的司机信息
            if(empty($driverInfo)){                             //判断缓存司机信息是否存在
                $driverInfo = $driverList;
            }else{
                //循环缓存中的司机信息
                $driverArrCache = [];
                foreach ($driverInfo as $key=>$value){
                    if(in_array($value['driverId'],$driverArr)){//缓存的司机ID 是否在当前的司机信息 ID中 存在 说明司机在线
                        $driverInfo[$key]['driverType'] = 1;

                        //在判断该司机坐标是否发生变化 已发生变化需要重新赋值
                        if($value['lng'] == $driverLocationArr[$value['driverId']]['lng'] && $value['lat'] == $driverLocationArr[$value['driverId']]['lat']){
                            $driverInfo[$key]['locationStatus'] = 0;        //司机坐标未发生变化 不更新坐标
                            $driverInfo[$key]['highLight']=false;
                            if ($highLight){
                                $driverInfo[$key]['highLight']=true;
                            }

                        }else{
                            $driverInfo[$key]['locationStatus'] = 1;        //司机坐标发生变化 更新坐标
                            $driverInfo[$key]['lng']            = $driverLocationArr[$value['driverId']]['lng'];        //将新的坐标 替换
                            $driverInfo[$key]['lat']            = $driverLocationArr[$value['driverId']]['lat'];        //将新的坐标 替换

                            $driverInfo[$key]['highLight']=false;
                            if ($highLight){
                                $driverInfo[$key]['highLight']=true;
                            }
                        }
                    }else{
                        $driverInfo[$key]['driverType'] = 0;                //不存在 说明该司机已经下线

                    }
                    $driverArrCache[] = $value['driverId'];
                }
                //反过来 将 当前司机信息 与 缓存司机信息进行比较
                foreach ($driverList as $key=>$value){
                    if(!in_array($value['driverId'],$driverArrCache)){      //司机id是否存在 - 存在
                        $value['driverType']        = 1;
                        $value['locationStatus']    = 2;                    //新增司机
                        $driverInfo[] = $value;
                    }
                }
            }
            Cache::set($driverInfoKey,$driverList);
            $count      = count($driverList);
            $data = [
                'provinces' => $driverInfo,
                'url'       => $request->domain(),
                'count'     => $count,
                'location'  => $location,
            ];
            return json_encode($data);die;
        }
        return $this->fetch('',['highLight'=>$highLight]);
    }
    //司机信息
    public function driverList(){
        //查询获取省份
        $page = input('get.page');
        $limit = input('get.limit');
        $data = input('get.');
        $data['driver_type']=1;

        $city_id        = $data['city_id']>0?$data['city_id']:133;
/*
 * [page] => 1
    [limit] => 10
    [province_id] => 13
    [city_id] => 133
    [driver_name] =>
    [driver_phone] =>
    [driver_type] => 1
 * */
        //$where="work_city=$city_id and driver_type=".$data['driver_type'];
        $where="work_city=$city_id";

        $where.=" and randNum_e_c=0";

        if($data['driver_phone']){
            $where.=" and driver_phone=".$data['driver_phone'];
        }
        $start = ($page -1)*$limit;
        $count = Db::table('xm_driver_location')->where($where)->count();
        $list = Db::table('xm_driver_location')->where($where)->limit($start,$limit)->order('ddNum desc')->select();
        $driverList=array();
        foreach($list as $k=>$v){
            $driverList[$k]['driverId']=$v['driver_id'];
            $driverList[$k]['name']=$v['driver_name'];
            $driverList[$k]['driver_phone']=$v['driver_phone'];
            $driverList[$k]['addressName']=$v['driver_id'];
            $driverList[$k]['time']=get_second_h_m($v['time_new'] - $v['time_old']);
            $driverList[$k]['orderCount']=$v['ddNum'];

            $url = "https://restapi.amap.com/v3/geocode/regeo?key=54fcf57140fcf3061dfa704083f4d8e0&location=".$v['lng'].','.$v['lat'];
            $addressArr = https_request($url);
            $addressArr = json_decode($addressArr,true);
            if(!empty($addressArr) && $addressArr['status'] == 1){
                $driverList[$k]['addressName'] = $addressArr['regeocode']['formatted_address'];
            }else{
                $driverList[$k]['addressName'] = $v['lng'].','.$v['lat'];
            }
        }

        $listArr=array(
            'code'  => 0,
            'msg'   => '',
            'count' => $count,
            'data'  => $driverList
        );
        echo json_encode($listArr);exit;

        $list = $this->fast_car_model->driverList($page,$limit,$data);
        echo json_encode($list);exit;
    }
    public function translator2(){
        $highLight  = input('id');
        $drivetType = input('post.drivetType');
        if(!empty($drivetType)){
            $where['b.driver_type']=2;
            $province_id    = input('post.province_id')?input('post.province_id'):13;
            $city_id        = input('post.city_id')?input('post.city_id'):133;
            $driverList = Db::name('car_info')->alias('a')->field('a.driver_id as driverId')->join('driver_base b','a.driver_id = b.driver_id','left')->where(['a.work_province'=>$province_id,'a.work_city'=>$city_id,'b.auth_status'=>1])->where($where)->select();


            $driverIdArr=array();
            foreach($driverList as $k=>$v){
                $driverIdArr[]=$v['driverId'];
            }
            $driverList=self::driverListMongo($driverIdArr);

            $cityLocationKey        = 'cityLocationKey2:'.$city_id;
            $cityLocalInfo          = Cache::get($cityLocationKey);
            if(!empty($cityLocalInfo)){
                $location           = $cityLocalInfo;
            }else{
                $cityName           = Db::name('region')->where(['region_id'=>$city_id])->value('region_name');
                $url                = "http://api.map.baidu.com/geocoder?address=".$cityName."&output=json&key=XgrsTKYvYuiq9wa2BUrfNXOW37E1DSdy&city=".$cityName;
                $result             = file_get_contents($url);
                $json               = json_decode($result,true);
                $location           = $json['result']['location'];
                Cache::set($cityLocationKey,$location);
            }

            //开始处理司机 - 数据
            $driverArr1 = $driverLocationArr = [];
            foreach ($driverList as $key=>$value){
                $driverArr1[]        = $value['driverId'];
                $driverLocationArr[$value['driverId']]['lng'] = $value['lng'];
                $driverLocationArr[$value['driverId']]['lat'] = $value['lat'];
            }
            //开始缓存司机信息
            $driverInfoKey          = 'driverlocation:info2';
            $driverInfo             = Cache::get($driverInfoKey);//获取缓存中的司机信息
            if(empty($driverInfo)){                             //判断缓存司机信息是否存在
                $driverInfo = $driverList;
            }else{
                //循环缓存中的司机信息
                $driverArrCache = [];
                foreach ($driverInfo as $key=>$value){
                    if(in_array($value['driverId'],$driverArr1)){//缓存的司机ID 是否在当前的司机信息 ID中 存在 说明司机在线
                        $driverInfo[$key]['driverType'] = 1;

                        //在判断该司机坐标是否发生变化 已发生变化需要重新赋值
                        if($value['lng'] == $driverLocationArr[$value['driverId']]['lng'] && $value['lat'] == $driverLocationArr[$value['driverId']]['lat']){
                            $driverInfo[$key]['locationStatus'] = 0;        //司机坐标未发生变化 不更新坐标
                            $driverInfo[$key]['highLight']=false;
                            if ($highLight){
                                $driverInfo[$key]['highLight']=true;
                            }
                        }else{
                            $driverInfo[$key]['locationStatus'] = 1;        //司机坐标发生变化 更新坐标
                            $driverInfo[$key]['lng']            = $driverLocationArr[$value['driverId']]['lng'];        //将新的坐标 替换
                            $driverInfo[$key]['lat']            = $driverLocationArr[$value['driverId']]['lat'];        //将新的坐标 替换

                            $driverInfo[$key]['highLight']=false;
                            if ($highLight){
                                $driverInfo[$key]['highLight']=true;
                            }
                        }
                    }else{
                        $driverInfo[$key]['driverType'] = 0;                //不存在 说明该司机已经下线

                    }
                    $driverArrCache[] = $value['driverId'];
                }
                //反过来 将 当前司机信息 与 缓存司机信息进行比较
                foreach ($driverList as $key=>$value){
                    if(!in_array($value['driverId'],$driverArrCache)){      //司机id是否存在 - 存在
                        $value['driverType']        = 1;
                        $value['locationStatus']    = 2;                    //新增司机
                        $driverInfo[] = $value;
                    }
                }
            }
            Cache::set($driverInfoKey,$driverList);
            $request                = Request::instance();
            $count      = count($driverList);
            $data = [
                'provinces' => $driverInfo,
                'url'       => $request->domain(),
                'count'     => $count,
                'location'  => $location,
            ];
            echo json_encode($data);die;
        }
        return $this->fetch('',['highLight'=>$highLight]);
    }
//司机信息
    public function driverList2(){
        //查询获取省份
        $page = input('get.page');
        $limit = input('get.limit');
        $data = input('get.');
        $data['driver_type']=2;
        $list = $this->fast_car_model->driverList($page,$limit,$data);
        echo json_encode($list);exit;
    }
    public function sendMsg(){
        Tools::sendMes('18555119143','2019/10/11 17:10:59',43);
    }

    public function driverListMongo($driverIdArr){
        $mongoCollection    = new Connection();
        $driverLocation     = $mongoCollection->table("live_driver_location")
            ->field('driverId,location,createTime,direction')
            ->where('driverId','in',$driverIdArr)
            ->select();

        $redis = new Redis();
        $driverArr = [];
        foreach($driverLocation as $key=>$v1){
            $hashKey                = 'driver_id:'.$v1['driverId'];
            if($redis->Exists($hashKey)){
                $driverLocal        = $redis->hGetAll($hashKey);
                if($driverLocal['lng'] == $v1['location']['coordinates'][0] && $driverLocal['lat'] == $v1['location']['coordinates'][1]){
                    $driverArr[$key]['localType']      = 1;
                    $driverArr[$key]['time']           = get_second_h_m(time() - $driverLocal['time']);
                    $driverArr[$key]['sumTime']        = time() - $driverLocal['time'];
                }else{
                    $driverArr[$key]['localType']      = 0;
                    $driverArr[$key]['time']           = '-';
                    $driverArr[$key]['sumTime']        = 0;
                    $driverLocal = [
                        'time'          => time(),
                        'lng'           => $v1['location']['coordinates'][0],
                        'lat'           => $v1['location']['coordinates'][1]
                    ];
                    $redis->hMSet($hashKey,$driverLocal);
                }
            }else{
                $driverArr[$key]['localType']          = 0;
                $driverArr[$key]['time']               = '-';
                $driverArr[$key]['sumTime']            = 0;
                $driverLocal = [
                    'time'          => time(),
                    'lng'           => $v1['location']['coordinates'][0],
                    'lat'           => $v1['location']['coordinates'][1]
                ];
                $redis->hMSet($hashKey,$driverLocal);
            }
            $driverArr[$key]['lng']        = $v1['location']['coordinates'][0];
            $driverArr[$key]['lat']        = $v1['location']['coordinates'][1];
            $driverArr[$key]['old_lng']    = !empty($driverLocal['lng'])?$driverLocal['lng']:'';
            $driverArr[$key]['old_lat']    = !empty($driverLocal['lat'])?$driverLocal['lat']:'';
            $driverArr[$key]['center']     = $v1['location']['coordinates'][0].','.$v1['location']['coordinates'][1];
            $driverArr[$key]['direction']  = $v1['direction'];
            $driverInfo = Db::name('driver_base')->where('driver_id='.$v1['driverId'])->find();
            $driverArr[$key]['name']  = $driverInfo['driver_name'];
            $driverArr[$key]['driver_phone']  = Security::decrypt($driverInfo['driver_phone']);
            $driverArr[$key]['driverId']  = $driverInfo['driver_id'];
        }
        return $driverArr;
    }

    public function work_city($city_id,$driver_type,$driver_id=null,$miao=0){
        //$where="work_city=$city_id and driver_type=$driver_type";
        $where="work_city=$city_id";
        if($driver_id){
            $where.=" and driver_id=$driver_id";
        }
        $driver_location_list=Db::name('driver_location')->where($where)->select();
        foreach($driver_location_list as $k=>$v){
            $arr=array();
            if($v['randNum_s_c']>0){
                $arr['randNum_s_c']=($v['randNum_s_c']-$miao)<=0?0:($v['randNum_s_c']-$miao);
                if($arr['randNum_s_c']==0){
                    $arr['randNum_e_c']=$v['randNum_e'];
                }else{
                    //在线
                    //随机是否移动
                    $rands=rand(0,100);
                    if($rands>40&&$rands<=70){
                        //有移动
                        //随机更新正负15°的坐标,系数0.001 return-> lng lat direction
                        $latlng['lat']=$v['lat'];
                        $latlng['lng']=$v['lng'];
                        $signBit15=self::signBit15($v['direction'],$latlng,$v['distance_old']);
                        $arr['distance_new']=$signBit15['distance'];
                        $arr['distance_old']=$v['distance_new'];
                        $arr['lat']=$signBit15['lat'];
                        $arr['lng']=$signBit15['lng'];
                        $arr['lat_old']=$v['lat'];
                        $arr['lng_old']=$v['lng'];
                        $arr['direction']=$signBit15['direction'];

                        $arr['time_old']=$v['time_new'];
                        $arr['time_new']=time();
                    }else{
                        $arr['lat']=$v['lat'];
                        $arr['lng']=$v['lng'];
                        $arr['lat_old']=$v['lat'];
                        $arr['lng_old']=$v['lng'];
                        $arr['time_old']=$v['time_new'];
                        $arr['time_new']=time();
                    }
                }
                if(rand(0,10)>5){
                    $s1=rand(1,10);
                    $s2=rand($s1,$s1*2);
                    $arr['ddNum']=rand($s1,$s2);//订单数
                }
            }else{
                $arr['randNum_e_c']=($v['randNum_e_c']-$miao)<=0?0:($v['randNum_e_c']-$miao);
                if($arr['randNum_e_c']==0){
                    $arr['randNum_s_c']=$v['randNum_s'];
                    $arr['lat_old']=0;
                    $arr['lng_old']=0;
                    $distance=rand(1,$this->distance_model);
                    $sss=$this->computationx(31.839051,117.423085, $v['direction'],$distance);
                    $arr['lat']=$sss['lat'];
                    $arr['lng']=$sss['lng'];

                }
            }
            Db::table('xm_driver_location')->where('driver_id',$v['driver_id'])->update($arr);
        }
        //$where.=" and randNum_e_c=0";
        $driver_location_list=Db::name('driver_location')->where($where)->select();
        $driverArr=array();
        foreach($driver_location_list as $key=>$val){
            $strArr=array();
            $strArr['center']=$val['lng'].','.$val['lat'];
            $strArr['direction']=$val['direction'];
            $strArr['driverId']=$val['driver_id'];
            $strArr['driverType']=$val['randNum_e_c']==0?1:0;//0司机已经下线 1在线
            $strArr['driver_phone']=$val['driver_phone'];
            $strArr['highLight']=false;
            if($driver_id){
                $strArr['highLight']=true;
            }
            $strArr['lat']=$val['lat'];
            $strArr['lng']=$val['lng'];
            $strArr['localType']=1;//没用到
            $strArr['locationStatus']=($val['lng']==$val['lng_old'])?0:1;//0不更新坐标 1更新坐标 2新增司机
            if($val['lng_old']==0){
                $strArr['locationStatus']=2;
            }
            $strArr['name']=$val['driver_name'];
            $strArr['old_lat']=$val['lat_old'];
            $strArr['old_lng']=$val['lng_old'];
            $strArr['sumTime']=$val['time_new'] - $val['time_old'];
            $strArr['time']=get_second_h_m($strArr['sumTime']);
            $latlng['lat']=$val['lat_old'];
            $latlng['lng']=$val['lng_old'];

            $driverArr[]=$strArr;
        }
        $count      = count($driverArr);
        $request                = Request::instance();

        $location['lng']=$this->lng_model;
        $location['lat']=$this->lat_model;
        $data = [
            'provinces' => $driverArr,
            'url'       => $request->domain(),
            'count'     => $count,
            'location'  => $location,
        ];
        return $data;
    }
    function signBit15($direction,$location,$distance){
        //117.281553,31.859653

        $lat1=$this->lat_model;
        $lng1=$this->lng_model;
        $lat2=$location['lat'];
        $lng2=$location['lng'];
        $direc=0;
        $yidong=rand(10,100);//每次移动的距离（米）
        $dd=self::getDistance($lat1, $lng1, $lat2, $lng2);

        if($dd>=($this->distance_model-4000)){
            if($dd>$distance){
                $direc=$direction+135;
            }else{
                $direc=$direction;
            }
        }else{
            $rands=rand(0,100);
            if($rands>40&&$rands<=70){
                $rands=rand(0,30);
                //大+，小-
                if($rands>15){
                    $direc=$direction+15;
                }else{
                    $direc=$direction-15;
                }
            }else{
                $direc=$direction;
            }

            $dd=$dd+$yidong;
            if($dd>=($this->distance_model-4000)){
                if($dd>$distance){
                    $direc=$direction+135;
                }else{
                    $direc=$direction;
                }
            }
        }
        if($direc<0){
            $direc=360+$direc;
        }elseif($direc>=360){
            $direc=$direc-360;
        }
        if($direc==0){
            $direc=1;
        }
        $location_new=self::computationx($lat2,$lng2,$direc,$yidong);
        $location_new['direction']=$direc;
        $location_new['distance']=$dd;
        return $location_new;

    }


    function rad($d){
        $Pi = pi();
        return $d*$Pi/180.0;
    }
    function computationx($STARTLAT, $STARTLONG, $ANGLE1, $DISTANCE){
        $Pi = pi();
        // return $Pi;
        $B1 = $STARTLAT;
        $L1 = $STARTLONG;
        $A1 = $ANGLE1;
        $S = $DISTANCE;
        $a = 6378245.0;
        $b = 6356752.3142;
        $c = $a*$a/$b;
        $alpha = ($a - $b) /$a;
        $e = sqrt($a *$a - $b *$b) /$a;
        $e2 = sqrt($a *$a - $b *$b) / $b;
        $B1 = $this->rad($B1);
        $L1 = $this->rad($L1);
        $A1 = $this->rad($A1);
        $W = sqrt(1 - $e *$e * (sin($B1) *sin($B1)));
        $V = $W * ($a / $b);
        //Dim W1 As Double
        $E1 = $e ;//第一偏心率
        //计算起点的归化纬度
        $W1 = $W;
        //Sqr(1 - e1 * e1 * Sin(B1 ) * Sin(B1 ))
        $sinu1 = sin($B1) * sqrt(1 - $E1 * $E1) / $W1;
        $cosu1 = cos($B1) / $W1;
        //计算辅助函数值
        $sinA0 = $cosu1 * sin($A1);
        $cotq1 = $cosu1 * cos($A1);
        $sin2q1 = 2 * $cotq1 / ($cotq1*$cotq1 + 1);
        $cos2q1 = ($cotq1 *$cotq1 - 1) / ($cotq1*$cotq1 + 1);
        //计算系数AA,BB,CC及AAlpha, BBeta的值。
        $cos2A0 = 1 - $sinA0 *$sinA0;
        $e2 = sqrt($a *$a - $b *$b) / $b;
        $k2 = $e2 * $e2 * $cos2A0;
        //Dim aa, BB, CC, EE22, AAlpha, BBeta As Double
        $aa = $b * (1 + $k2 / 4 - 3 * $k2 * $k2 / 64 + 5 * $k2 * $k2 * $k2 / 256);
        $BB = $b * ($k2 / 8 - $k2 * $k2 / 32 + 15 * $k2 * $k2 * $k2 / 1024);
        $CC = $b * ($k2 * $k2 / 128 - 3 * $k2 * $k2 * $k2 / 512);
        $e2 = $E1 * $E1;
        $AAlpha = ($e2 / 2 + $e2 * $e2 / 8 + $e2 * $e2 * $e2 / 16) - ($e2 * $e2 / 16 + $e2 * $e2 * $e2 / 16) * $cos2A0 + (3 * $e2 * $e2* $e2 / 128) * $cos2A0 * $cos2A0;
        $BBeta = ($e2 * $e2 / 32 + $e2 * $e2 * $e2 / 32) * $cos2A0 - ($e2 * $e2 * $e2 / 64) * $cos2A0 * $cos2A0;
        //计算球面长度
        $q0 = ($S - ($BB + $CC * $cos2q1) * $sin2q1) / $aa;
        $sin2q1q0 = $sin2q1 * cos(2 * $q0) + $cos2q1 * sin(2 * $q0);
        $cos2q1q0 = $cos2q1 * cos(2 * $q0) - $sin2q1 * sin(2 * $q0);
        $q = $q0 + ($BB + 5 * $CC * $cos2q1q0) * $sin2q1q0 / $aa;
        //'// 计算经度差改正数
        $theta = ($AAlpha * $q + $BBeta * ($sin2q1q0 - $sin2q1)) * $sinA0;
        //计算终点大地坐标及大地方位角
        $sinu2 = $sinu1 * cos($q) + $cosu1 * cos($A1) * sin($q);
        $B2 = atan($sinu2 / (sqrt(1 - $E1 * $E1) * sqrt(1 - $sinu2 * $sinu2))) * 180 / $Pi;
        $lamuda = atan(sin($A1) * sin($q) / ($cosu1 * cos($q) - $sinu1 * sin($q) * cos($A1))) * 180 / $Pi;
        if (sin($A1) > 0){
            if (sin($A1) * sin($q) / ($cosu1 * cos($q) - $sinu1 * sin($q) * cos($A1)) > 0){
                $lamuda = abs($lamuda);
            }else{
                $lamuda = 180 - abs($lamuda);}
        }else{
            if (sin($A1) * sin($q) / ($cosu1 * cos($q) - $sinu1 * sin($q) * cos($A1)) > 0){
                $lamuda = abs($lamuda) - 180;}
            else{
                $lamuda = -abs($lamuda);}
        }
        $L2 = $L1 * 180 / $Pi + $lamuda - $theta * 180 / $Pi;
        $A2 = atan($cosu1 * sin($A1) / ($cosu1 * cos($q) * cos($A1) - $sinu1 * sin($q))) * 180 / $Pi;
        if (sin($A1) > 0){
            if ($cosu1 * sin($A1) / ($cosu1 * cos($q) * cos($A1) - $sinu1 * sin($q)) > 0){
                $A2 = 180 + abs($A2);}else{
                $A2 = 360 - abs($A2);}
        }else{
            if ($cosu1 * sin($A1) / ($cosu1 * cos($q) * cos($A1) - $sinu1 * sin($q)) > 0){
                $A2 = abs($A2);
            }else{
                $A2 = 180 - abs($A2);
            }
        }//A2代表北偏西的角度
        $L2=round($L2,6);
        $B2=round($B2,6);
        $location=array("lng"=>$L2,"lat"=>$B2);
        return $location;//返回坐标数组
    }

    public function generateName(){
        $arrXing = $this->getXingList();
        $numbXing = count($arrXing);
        $arrMing = $this->getMingList();
        $numbMing =  count($arrMing);

        $Xing = $arrXing[mt_rand(0,$numbXing-1)];
        $Ming = $arrMing[mt_rand(0,$numbMing-1)];
        $sj=rand(1,100);
        if($sj>=35&&$sj<=75){
            $Ming .= $arrMing[mt_rand(0,$numbMing-1)];
        }
        $name = $Xing.$Ming;

        return $name;

    }
    //获取姓氏
    public function getXingList(){
        $arrXing=array('赵','钱','孙','李','周','吴','郑','王','冯','陈','褚','卫','蒋','沈','韩','杨','朱','秦','尤','许','何','吕','施','张','孔','曹','严','华','金','魏','陶','姜','戚','谢','邹',
            '喻','柏','水','窦','章','云','苏','潘','葛','奚','范','彭','郎','鲁','韦','昌','马','苗','凤','花','方','任','袁','柳','鲍','史','唐','费','薛','雷','贺','倪','汤','滕','殷','罗',
            '毕','郝','安','常','傅','卞','齐','元','顾','孟','平','黄','穆','萧','尹','姚','邵','湛','汪','祁','毛','狄','米','伏','成','戴','谈','宋','茅','庞','熊','纪','舒','屈','项','祝',
            '董','梁','杜','阮','蓝','闵','季','贾','路','娄','江','童','颜','郭','梅','盛','林','钟','徐','邱','骆','高','夏','蔡','田','樊','胡','凌','霍','虞','万','支','柯','管','卢','莫',
            '柯','房','裘','缪','解','应','宗','丁','宣','邓','单','杭','洪','包','诸','左','石','崔','吉','龚','程','嵇','邢','裴','陆','荣','翁','荀','于','惠','甄','曲','封','储','仲','伊',
            '宁','仇','甘','武','符','刘','景','詹','龙','叶','幸','司','黎','溥','印','怀','蒲','邰','从','索','赖','卓','屠','池','乔','胥','闻','莘','党','翟','谭','贡','劳','逄','姬','申',
            '扶','堵','冉','宰','雍','桑','寿','通','燕','浦','尚','农','温','别','庄','晏','柴','瞿','阎','连','习','容','向','古','易','廖','庾','终','步','都','耿','满','弘','匡','国','文',
            '寇','广','禄','阙','东','欧','利','师','巩','聂','关','荆','司马','上官','欧阳','夏侯','诸葛','闻人','东方','赫连','皇甫','尉迟','公羊','澹台','公冶','宗政','濮阳','淳于','单于','太叔',
            '申屠','公孙','仲孙','轩辕','令狐','徐离','宇文','长孙','慕容','司徒','司空');
        return $arrXing;

    }
    //获取名字
    public function getMingList(){
        $arrMing=array('伟','刚','勇','毅','俊','峰','强','军','平','保','东','文','辉','力','明','永','健','世','广','志','义','兴','良','海','山','仁','波','宁','贵','福','生','龙','元','全'
        ,'国','胜','学','祥','才','发','武','新','利','清','飞','彬','富','顺','信','子','杰','涛','昌','成','康','星','光','天','达','安','岩','中','茂','进','林','有','坚','和','彪','博','诚'
        ,'先','敬','震','振','壮','会','思','群','豪','心','邦','承','乐','绍','功','松','善','厚','庆','磊','民','友','裕','河','哲','江','超','浩','亮','政','谦','亨','奇','固','之','轮','翰'
        ,'朗','伯','宏','言','若','鸣','朋','斌','梁','栋','维','启','克','伦','翔','旭','鹏','泽','晨','辰','士','以','建','家','致','树','炎','德','行','时','泰','盛','雄','琛','钧','冠','策'
        ,'腾','楠','榕','风','航','弘','秀','娟','英','华','慧','巧','美','娜','静','淑','惠','珠','翠','雅','芝','玉','萍','红','娥','玲','芬','芳','燕','彩','春','菊','兰','凤','洁','梅','琳'
        ,'素','云','莲','真','环','雪','荣','爱','妹','霞','香','月','莺','媛','艳','瑞','凡','佳','嘉','琼','勤','珍','贞','莉','桂','娣','叶','璧','璐','娅','琦','晶','妍','茜','秋','珊','莎'
        ,'锦','黛','青','倩','婷','姣','婉','娴','瑾','颖','露','瑶','怡','婵','雁','蓓','纨','仪','荷','丹','蓉','眉','君','琴','蕊','薇','菁','梦','岚','苑','婕','馨','瑗','琰','韵','融','园'
        ,'艺','咏','卿','聪','澜','纯','毓','悦','昭','冰','爽','琬','茗','羽','希','欣','飘','育','滢','馥','筠','柔','竹','霭','凝','晓','欢','霄','枫','芸','菲','寒','伊','亚','宜','可','姬'
        ,'舒','影','荔','枝','丽','阳','妮','宝','贝','初','程','梵','罡','恒','鸿','桦','骅','剑','娇','纪','宽','苛','灵','玛','媚','琪','晴','容','睿','烁','堂','唯','威','韦','雯','苇','萱'
        ,'阅','彦','宇','雨','洋','忠','宗','曼','紫','逸','贤','蝶','菡','绿','蓝','儿','翠','烟');
        return $arrMing;
    }

    public function SJtel(){
        $arr = array(
            130,131,132,133,134,135,136,137,138,139,
            144,147,
            150,151,152,153,155,156,157,158,159,
            176,177,178,
            180,181,182,183,184,185,186,187,188,189,
        );
        return $arr[array_rand($arr)].mt_rand(1000,9999).mt_rand(1000,9999);
    }

    /**
     * @desc 根据两点间的经纬度计算距离
     * @param float $lat 纬度值
     * @param float $lng 经度值
     */
    function getDistance($lat1, $lng1, $lat2, $lng2)
    {
        $earthRadius = 6371000;//6367000
        $lat1 = ($lat1 * pi() ) / 180;
        $lng1 = ($lng1 * pi() ) / 180;
        $lat2 = ($lat2 * pi() ) / 180;
        $lng2 = ($lng2 * pi() ) / 180;
        $calcLongitude = $lng2 - $lng1;
        $calcLatitude = $lat2 - $lat1;
        $stepOne = pow(sin($calcLatitude / 2), 2) + cos($lat1) * cos($lat2) * pow(sin($calcLongitude / 2), 2);
        $stepTwo = 2 * asin(min(1, sqrt($stepOne)));
        $calculatedDistance = $earthRadius * $stepTwo;
        return round($calculatedDistance);
    }

}
